//
//  CBPickerView.m
//  TF_manPickerView
//
//  Created by 韦朝宾 on 16/5/27.
//  Copyright © 2016年 TF_man. All rights reserved.
//

#import "YJDatePickerView.h"

//工具条高度
#define TOOLBARH 44
//时间选择器的高度
#define DATEPICKERVIEWHEIGHT 216

@interface YJDatePickerView ()
@property(nonatomic,weak)UIDatePicker *datePicker;
@property(nonatomic,weak)UIView *toolBar;
@end
@implementation YJDatePickerView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        //顶部工具条
        UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, IPHONE_H, IPHONE_W, TOOLBARH)];
        toolBar.backgroundColor = [UIColor whiteColor];
        [self addSubview:toolBar];
        _toolBar = toolBar;
        //取消
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 50, toolBar.height);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:MYCOLOR(88, 144, 255) forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:cancelBtn];
        
        //确认
        UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        completeBtn.frame = CGRectMake(toolBar.width - 50, 0, 50, toolBar.height);
        [completeBtn setTitle:@"完成" forState:UIControlStateNormal];
        [completeBtn setTitleColor:MYCOLOR(88, 144, 255) forState:UIControlStateNormal];
        [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:completeBtn];
        
        //标题
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(cancelBtn.width, 0, toolBar.width - 2 *cancelBtn.width, toolBar.height)];
        label.text = @"出生年月日";
        label.font = [UIFont systemFontOfSize:18.0f];
        label.textAlignment = NSTextAlignmentCenter;
        [toolBar addSubview:label];
 
        //分割线
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, toolBar.height - 1, toolBar.width, 1)];
        line.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
        [toolBar addSubview:line];
        
        //datePicker
        UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, IPHONE_H + TOOLBARH, IPHONE_W, DATEPICKERVIEWHEIGHT)];
        datePicker.backgroundColor = [UIColor whiteColor];
        
        //设置格式和区域
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        //设置可选时间段
        NSDateFormatter *fmt = [NSDateFormatter new];
        fmt.dateFormat = @"yyyy-MM-dd";
        datePicker.minimumDate = [fmt dateFromString:@"1900-01-01"];
        datePicker.maximumDate = [NSDate date];
        
        [self addSubview:datePicker];
        _datePicker = datePicker;
        
    }
    return self;
}

- (void)show
{

    UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
    [win addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        _toolBar.transform = CGAffineTransformMakeTranslation(0, - DATEPICKERVIEWHEIGHT - TOOLBARH);
        _datePicker.transform = CGAffineTransformMakeTranslation(0,  - DATEPICKERVIEWHEIGHT - TOOLBARH);
    }];
}

- (void)cancelBtnClick
{
    [self dismiss];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
        _toolBar.transform = CGAffineTransformIdentity;
        _datePicker.transform = CGAffineTransformIdentity;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//完成点击事件
- (void)completeBtnClick
{
   
    if (self.completeBlock)
        self.completeBlock(_datePicker.date);
    [self dismiss];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}


@end
